package com.mpyy.handler.systemSetting;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.mpyy.pojo.*;
import com.mpyy.service.systemSetting.OperatorServiceInter;
import com.mpyy.tool.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName:com.mpyy.handler.systemSetting
 * @ClassName:OperatorHandler
 * @Date:2022-03-30:15:56
 * @Author: Tang Neng
 */
@Controller
public class OperatorHandler {
    @Autowired
    private OperatorServiceInter operatorServiceInter;
    @RequestMapping(value = "/loginOperatorInfo",method = RequestMethod.POST , produces = "appliction/json;charset=UTF-8")
    @ResponseBody
    public String loginOperatorInfo(OperatorInfo operatorInfo, HttpSession session){
        OperatorInfo operatorInfos = operatorServiceInter.searchOperatorLoginInfo(operatorInfo);
        session.setAttribute("USER_OPERATOR",operatorInfos);
        return JSON.toJSONString(operatorInfos);
    }
    /**树形控件加载**/
    @RequestMapping(value = {"/searchProdutType"},method = RequestMethod.GET, produces = "text/html;charset=utf-8")
    @ResponseBody
    public String searchTreeControlProcess(){
        //商品类别数据
        List<ProductType> productTypes = operatorServiceInter.searchTreeControl();

        //新建一个集合用于将数据转化为符合tree的格式
        List<ProductTypeTree> trees = new ArrayList<>();

        //1.将数据转存至productTypeTree类型的List中（tree对于数据的键有一定要求）
        for (ProductType productType : productTypes) {

            ProductTypeTree productTypeTree = new ProductTypeTree(
                    productType.getProTypeId(),productType.getProTypeName(),
                    productType.getProLevelParId(),null
            );
            //将数据添加到集合中
            trees.add(productTypeTree);
        }

        //创建一个新集合用于保存处理过后的数据 并为其添加子节点数据
        List<ProductTypeTree> tree = new ArrayList<ProductTypeTree>();

        for (ProductTypeTree typeTree : trees) {
            //对象的上级编号为null(ProLevelParId()== null) 当前为父级
            if (typeTree.getProLevelParId()==null){
                //调用子节点方法并将数据追加至集合中
                tree.add(findChildren(typeTree,trees));
            }
        }
        //创建Map集合 将数据转换为JSON格式并将其传递给页面
        Map<Object,Object> map = new HashMap<>();
        map.put("data",tree);
        return JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect);

    }

    /** 查找子节点的方法*/
    public static ProductTypeTree findChildren(ProductTypeTree treeNode, List<ProductTypeTree> treeNodes){
        //1.遍历整个保存数据的集合
        for (ProductTypeTree tree : treeNodes){
            //1.1如果传递过来的父节点的id与某节点的父id相同，则表示它们存在父子关系
            if (treeNode.getId().equals(tree.getProLevelParId())) {
                //判断父节点的Children集合是否为空
                if(treeNode.getChildren()==null){
                    //如果为空 新建一个集合 new ArrayList<ProductTypeTree>()
                    treeNode.setChildren(new ArrayList<ProductTypeTree>());
                }
                //继续寻找当前子节点的下一级，寻找完毕后加入当前父节点
                treeNode.getChildren().add(findChildren(tree,treeNodes));
            }
        }
        return treeNode;
    }

    /**根据类别查询商品信息*/
    @RequestMapping(value = {"/searchCommodityDetaInfoils"},method = RequestMethod.GET , produces = "text/html;charset=utf-8")
    @ResponseBody
    public String searchCommodityDetaInfoils(String proTypeId , Paging paging){
        List<ProductInfo> productInfos = operatorServiceInter.searchCommodityClassInfo(proTypeId, paging);
        List<ProductInfo> productInfosSize = operatorServiceInter.searchCommodityClassInfo(proTypeId, null);
        return Tool.hashMapInfo(productInfosSize , productInfos);
    }
     
    /**显示商品售价明细数据*/
    @RequestMapping(value = "/searchCommodityClassDescInfo" , method = RequestMethod.GET,produces = "text/html;charset=utf-8")
    @ResponseBody
    public String searchCommodityClassDescInfo(String proId){
        List<MenmberTypeld> menmberTypeld = operatorServiceInter.searchCommodityClassDescInfo(proId);
        System.out.println(proId);
        for (MenmberTypeld menmberTypeldS : menmberTypeld) {
            System.out.println(menmberTypeldS);
        }
        return Tool.hashMapInfo(menmberTypeld , menmberTypeld);
    }

    /**修改商品价格*/
    @RequestMapping(value = {"/updateCommodityPriecInfo"} , method = RequestMethod.POST)
    @ResponseBody
    public int updateCommodityPriecInfo(ProductInfo productInfo){
        System.out.println(productInfo);
        int i = operatorServiceInter.updateCommodityPriceInfo(productInfo);
        return i;
    }
}
